﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using TfsBranchLib2.Model;

namespace TfsBranchLib2.View
{
    /// <summary>
    ///     Interaction logic for BranchedHistoryInputControl.xaml
    /// </summary>
    public partial class BranchedHistoryInputControl : UserControl
    {
        private BranchedHistoryContext branchedHistoryContext;

        public BranchedHistoryInputControl()
        {
            InitializeComponent();
        }

        internal void SetBindings(BranchedHistoryContext branchedHistoryContext)
        {
            this.branchedHistoryContext = branchedHistoryContext;
            sourceControlPathsBox.SetBindings(branchedHistoryContext);
            branchListBox.SetBindings(branchedHistoryContext);
            settingsBar.SetMinComponent(branchedHistoryContext);
            branchedHistoryContext.GenerateHistoryListCommand.ApplyToButton(StartButton);
            branchedHistoryContext.StopQueryCommand.ApplyToButton(CancelButton);
            branchedHistoryContext.OnProcessCompleted += branchedHistoryContext_OnProcessCompleted;
            branchedHistoryContext.OnProcessStarted += branchedHistoryContext_OnProcessStarted;
            progressBar.SetBinding(RangeBase.ValueProperty, new Binding("Progress")
            {
                Mode = BindingMode.OneWay,
                Source = branchedHistoryContext
            });
        }

        private void branchedHistoryContext_OnProcessStarted(object sender, EventArgs e)
        {
            Dispatcher.Invoke(() =>
            {
                CancelButton.IsEnabled = true;
                sourceControlPathsBox.IsEnabled = false;
                branchListBox.IsEnabled = false;
            });
        }

        private void branchedHistoryContext_OnProcessCompleted(object sender, EventArgs e)
        {
            Dispatcher.Invoke(() =>
            {
                CancelButton.IsEnabled = false;
                sourceControlPathsBox.IsEnabled = true;
                branchListBox.IsEnabled = true;
            });
        }

        private void sourceControlPathsBox_Loaded(object sender, RoutedEventArgs e)
        {
        }
    }
}